package com.truckdispatching.activity;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.truckdispatching.R;
import com.truckdispatching.UserManager;
import com.truckdispatching.util.ProvinceCityUtil;
import com.truckdispatching.util.SharedPreferencesUtil;

/**
 * 闪屏界面
 * 
 * @author Lee
 * @date 2013-4-30
 */
public class SplashActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_activity);
		new InitThread().start();
		SharedPreferencesUtil.getInstance().initWithApplication(getApplication());
		InitHandler handler = new InitHandler();
		handler.sendEmptyMessageDelayed(0, 2000);
	}

	private class InitThread extends Thread {
		@Override
		public void run() {
			int count = 5;
			while (count >= 0) {
				try {
//					ProvinceCityUtil.importXml(getAssets().open("area.xml"));
					ProvinceCityUtil.importData();
					break;
				} catch (Exception e) {
					count--;
				}
			}
		}
	}

	private class InitHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			boolean isLogin = UserManager.getIsLogin();
			if (isLogin) {
				Intent intent = new Intent(SplashActivity.this, MainActivity.class);
				startActivity(intent);
			} else {
				Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
				startActivity(intent);
			}
			finish();
		}
	}
}
